package com.home.databinding3

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        var recyclerview = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerview.layoutManager = LinearLayoutManager(this)
        val list:MutableList<String> = ArrayList()
        list.add("asd")
        list.add("asd")
        list.add("asd")
        list.add("asd")
        recyclerview.adapter = Adatper(list)
        list.add("zzm")
        recyclerview.adapter?.notifyDataSetChanged()
    }
    inner class Adatper(private val list: MutableList<String>) : RecyclerView.Adapter<Adatper.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adatper.ViewHolder {
            val inflate = layoutInflater.inflate(R.layout.demo_item, null)
            return ViewHolder(inflate)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: Adatper.ViewHolder, position: Int) {
            holder.itemView.findViewById<TextView>(R.id.textView).text = list[position]
        }
        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        }
    }
}